<?php


namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller;

class FileController  extends Controller
{
    public function uploadImage(){
        $request=request();
        if ($request->hasFile('file') && $request->file('file')->isValid()) {
            $maxSize = 1;
            //支持的上传图片类型
            $allowed_extensions = ["png", "jpg", "gif","jpeg"];
            $file = $request->file('file');
            $ext = $file->getClientOriginalExtension();
            if (!in_array(strtolower($ext),$allowed_extensions)){
                appMessage(1,"请上传".implode(",",$allowed_extensions)."格式的图片");
            }
            //检测图片大小
            if ($file->getClientSize() > $maxSize*1024*1024){
                appMessage(1,"图片大小限制".$maxSize."M");
            }
            $store_result = $file->store('public/'.date('Ymd'));
            if($store_result){
                $attachment=substr($store_result,7);
                $data = [
                    'url'  =>getenv('APP_URL').'storage/'.$attachment,
                    'attachment'=>$attachment
                ];
                appMessage(0,'ok',$data);
            }
        }
        appMessage(1,"未获取到上传文件或上传过程出错");
    }
}